<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    // 检查是否被踢出
    String kickMessage = (String) session.getAttribute("kickMessage");
    if (kickMessage != null) {
        request.setAttribute("error", kickMessage);
        session.removeAttribute("kickMessage");
        session.removeAttribute("kickedOut");
    }

    // 检查是否是退出登录
    String success = request.getParameter("success");
    if ("logout".equals(success)) {
        request.setAttribute("success", "您已成功退出登录！");
    }
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户登录 - 学生信息管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .login-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.3);
            padding: 40px;
            width: 100%;
            max-width: 420px;
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .login-header p {
            color: #666;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: bold;
            font-size: 14px;
        }

        .required {
            color: red;
        }

        .input-wrapper {
            position: relative;
        }

        .input-wrapper input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s;
        }

        .input-wrapper input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .verify-code-group {
            display: flex;
            gap: 10px;
            align-items: flex-end;
        }

        .verify-code-input {
            flex: 1;
        }

        .verify-code-img {
            width: 120px;
            height: 40px;
            cursor: pointer;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            transition: all 0.3s;
        }

        .verify-code-img:hover {
            border-color: #667eea;
            transform: scale(1.05);
        }

        .error-message {
            background: #fee;
            color: #c33;
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #c33;
            font-size: 14px;
        }

        .success-message {
            background: #efe;
            color: #3c3;
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #3c3;
            font-size: 14px;
        }

        .btn-login {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s;
        }

        .btn-login:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .btn-login:active {
            transform: translateY(0);
        }

        .login-footer {
            margin-top: 20px;
            text-align: center;
            color: #666;
            font-size: 13px;
        }

        .login-tips {
            background: #f0f7ff;
            padding: 15px;
            border-radius: 8px;
            margin-top: 20px;
            border-left: 4px solid #667eea;
        }

        .login-tips h4 {
            color: #667eea;
            margin-bottom: 8px;
            font-size: 14px;
        }

        .login-tips p {
            color: #666;
            font-size: 12px;
            line-height: 1.6;
            margin: 3px 0;
        }
    </style>
    <script>
        // 刷新验证码
        function refreshCode() {
            var img = document.getElementById("verifyCodeImg");
            img.src = "${pageContext.request.contextPath}/verifyCode?t=" + new Date().getTime();
        }

        // 表单验证
        function validateForm() {
            var username = document.getElementById("username").value.trim();
            var password = document.getElementById("password").value.trim();
            var verifyCode = document.getElementById("verifyCode").value.trim();

            if (username === "") {
                alert("请输入用户名！");
                return false;
            }

            if (password === "") {
                alert("请输入密码！");
                return false;
            }

            if (verifyCode === "") {
                alert("请输入验证码！");
                return false;
            }

            if (verifyCode.length !== 4) {
                alert("验证码长度应为4位！");
                return false;
            }

            return true;
        }

        // 页面加载完成后自动聚焦到用户名输入框
        window.onload = function() {
            document.getElementById("username").focus();
        };
    </script>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h1>用户登录</h1>
            <p>学生信息管理系统</p>
        </div>

        <!-- 错误消息 -->
        <c:if test="${not empty error}">
            <div class="error-message">${error}</div>
        </c:if>

        <!-- 成功消息 -->
        <c:if test="${not empty success}">
            <div class="success-message">${success}</div>
        </c:if>

        <form action="${pageContext.request.contextPath}/login" method="post" onsubmit="return validateForm()">
            <div class="form-group">
                <label for="username">用户名 <span class="required">*</span></label>
                <div class="input-wrapper">
                    <input type="text" id="username" name="username"
                           placeholder="请输入用户名" maxlength="50"
                           value="${param.username}">
                </div>
            </div>

            <div class="form-group">
                <label for="password">密码 <span class="required">*</span></label>
                <div class="input-wrapper">
                    <input type="password" id="password" name="password"
                           placeholder="请输入密码" maxlength="50">
                </div>
            </div>

            <div class="form-group">
                <label for="verifyCode">验证码 <span class="required">*</span></label>
                <div class="verify-code-group">
                    <div class="input-wrapper verify-code-input">
                        <input type="text" id="verifyCode" name="verifyCode"
                               placeholder="请输入验证码" maxlength="4">
                    </div>
                    <img id="verifyCodeImg"
                         src="${pageContext.request.contextPath}/verifyCode"
                         class="verify-code-img"
                         onclick="refreshCode()"
                         title="点击刷新验证码"
                         alt="验证码">
                </div>
            </div>

            <button type="submit" class="btn-login">登 录</button>
        </form>

        <div class="login-footer">
            <p>© 2024 学生信息管理系统</p>
        </div>

        <div class="login-tips">
            <h4>测试账号：</h4>
            <p>管理员 - 用户名: admin, 密码: 123456</p>
            <p>普通用户 - 用户名: user1, 密码: 123456</p>
            <p>普通用户 - 用户名: user2, 密码: 123456</p>
        </div>
    </div>
</body>
</html>

